﻿/************************************
*************************************
       自选行情显示页面
*************************************
*************************************/
#include "marketselfwidget.h"
#include"UFX/analyStr.h"


MarketSelfWidget::MarketSelfWidget(QWidget *parent) :
    QWidget(parent)
{
    setupModel();
    setupViews();

    m_strCurrContract = "";
    connect(m_tableView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(SelectSelfContract(const QModelIndex &)));
}

void MarketSelfWidget::setupModel()
{
    m_MarketselfDelegate = new MarketSelfDelegate;
    m_tableView = new TableView(m_MarketselfDelegate);

    QStringList header;
    header << QStringLiteral("合约代码") << QStringLiteral("最新价") << QStringLiteral("买价")
           << QStringLiteral("买量") << QStringLiteral("卖价") << QStringLiteral("卖量");

    header << QStringLiteral("期货收盘价") << QStringLiteral("交易所") << QStringLiteral("成交量")
           << QStringLiteral("持仓量") << QStringLiteral("最高") << QStringLiteral("最低");

    header << QStringLiteral("开盘价") << QStringLiteral("昨结算") << QStringLiteral("涨停板")
           << QStringLiteral("跌停板") << QStringLiteral("均价")<< QStringLiteral("涨跌");
    m_tableView->initHeader(header);
}

void MarketSelfWidget::setupViews()
{
    MainLayout = new QGridLayout(this);
    MainLayout->addWidget(m_tableView,0,0);
    MainLayout->setMargin(0);

    connect(this,SIGNAL(UpdateRow(int,QStringList)),m_tableView,SLOT(UpdateRow(int,QStringList)));
}

void MarketSelfWidget::SelectSelfContract(const QModelIndex &index){
    int iRow = index.row();
    //合约
    QModelIndex oContract = index.sibling(iRow,0);
    m_strCurrContract = oContract.data().toString();
}

double MarketSelfWidget::GetBuy1Price(QString code){
	if(m_MapBuy1Price.contains(code)){
		return m_MapBuy1Price[code];
	}
	return 0;
}

double MarketSelfWidget::GetSell1Price(QString code){
	if (m_MapSell1Price.contains(code))
	{
		return m_MapSell1Price[code];
	}
	return 0;
}
double MarketSelfWidget::GetLastPrice(QString code){
    if (m_MapLastPrice.contains(code))
    {
        return m_MapSell1Price[code];
    }
    return 0;
}

void MarketSelfWidget::ShowDepthMarketData(const char* msg){
    char cTime[1024];
    CDate oNowDate;
    oNowDate.GetTime(cTime,':');

    const char *data_only_ptr = msg;
    int data_only_len = strlen(data_only_ptr);
    char* buf = (char*)malloc(data_only_len + 1);
    memcpy(buf, data_only_ptr, data_only_len);
    buf[data_only_len] = '\0';

    CMarketInfo_UFX m_pMarketinfo;
    ParseMarketInfo_UFX(&m_pMarketinfo, buf);

	if(m_MapBuy1Price.contains(QString(m_pMarketinfo.contract_code))){
		m_MapBuy1Price[QString(m_pMarketinfo.contract_code)] = m_pMarketinfo.buy_high_price;
	}else{
		m_MapBuy1Price.insert(QString(m_pMarketinfo.contract_code), m_pMarketinfo.buy_high_price);
	}
	if(m_MapSell1Price.contains(QString(m_pMarketinfo.contract_code))){
		m_MapSell1Price[QString(m_pMarketinfo.contract_code)] = m_pMarketinfo.sale_low_price;
	}else{
		m_MapSell1Price.insert(QString(m_pMarketinfo.contract_code), m_pMarketinfo.sale_low_price);
	}
    if(m_MapLastPrice.contains(QString(m_pMarketinfo.contract_code))){
        m_MapLastPrice[QString(m_pMarketinfo.contract_code)] = m_pMarketinfo.futu_last_price;
    }else{
        m_MapLastPrice.insert(QString(m_pMarketinfo.contract_code), m_pMarketinfo.futu_last_price);
    }

    if(m_strCurrContract == QString(m_pMarketinfo.contract_code)){
        emit SigControlWgtInfo(m_pMarketinfo.buy_high_price,m_pMarketinfo.buy_high_amount,m_pMarketinfo.sale_low_price,m_pMarketinfo.sale_low_amount);
    }

    QStringList row_list;
    //单合约行情显示
    row_list << QString(m_pMarketinfo.contract_code);///合约代码
    row_list << QString::number(m_pMarketinfo.futu_last_price);///最新价
    ///申买价一
    row_list << QString::number(m_pMarketinfo.buy_high_price);
    ///申买量一
    row_list << QString::number(m_pMarketinfo.buy_high_amount);
    ///申卖价一
    row_list << QString::number(m_pMarketinfo.sale_low_price);
    ///申卖量一
    row_list << QString::number(m_pMarketinfo.sale_low_amount);
    ///期货收盘价
    row_list << QString::number(m_pMarketinfo.futu_close_price);
    ///交易所
    row_list << QString(m_pMarketinfo.futu_exch_type);
    ///数量
    row_list << QString::number(m_pMarketinfo.business_amount);
    ///持仓量
    row_list << QString::number(m_pMarketinfo.open_interest);
    ///最高价
    row_list << QString::number(m_pMarketinfo.futu_high_price);
    ///最低价
    row_list << QString::number(m_pMarketinfo.futu_low_price);
    ///今开盘
    row_list << QString::number(m_pMarketinfo.futu_open_price);
    ///上次结算价
    row_list << QString::number(m_pMarketinfo.pre_settlement_price);
    ///涨停板价
    row_list << QString::number(m_pMarketinfo.uplimited_price);
    ///跌停板价
    row_list << QString::number(m_pMarketinfo.downlimited_price);
    ///当日均价
    row_list << QString::number(m_pMarketinfo.average_price);

    if( buf != NULL)
        free(buf);
	
    int iRowCnt = m_tableView->rowCount();
	for (int i = 0; i < iRowCnt; i++)
	{
        QModelIndex InstrumentIDIndex  = m_tableView->model->index(i,0);
        QString InstrumentID = m_tableView->model->data(InstrumentIDIndex,Qt::DisplayRole).toString();
		QString str = QString(m_pMarketinfo.contract_code);
		if (InstrumentID == str)
		{
            QModelIndex lastPriceIndex  = m_tableView->model->index(i,1);
            double oldLastPrice = m_tableView->model->data(lastPriceIndex,Qt::DisplayRole).toDouble();
			if (m_pMarketinfo.futu_last_price >= oldLastPrice){
				row_list<<"1";//涨
			}else{
				row_list<<"0";//跌
			}
			emit UpdateRow(i,row_list);
			return;
		} 
	}
    if (row_list.size() < 18)
    {
        row_list<<"1";
    }

    m_tableView->addRow(row_list);
}
